﻿// Decompiled with JetBrains decompiler
// Type: Commom.Util.EnumUtil
// Assembly: MT5Trade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8045A2CB-DBD6-41AD-8804-F98CAC1BDC8D
// Assembly location: C:\projects\trade\trade_tool\trade0812\MT5Trade.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

#nullable enable
namespace Commom.Util;

public static class EnumUtil
{
  public static TEnum Parse<TEnum>(object member)
  {
    string str = member.SafeString();
    if (!string.IsNullOrWhiteSpace(str))
      return (TEnum) Enum.Parse(Common.GetType<TEnum>(), str, true);
    if (typeof (TEnum).IsGenericType)
      return default (TEnum);
    throw new ArgumentNullException(nameof (member));
  }

  public static string GetName<TEnum>(object member)
  {
    return EnumUtil.GetName(Common.GetType<TEnum>(), member);
  }

  public static string GetName(Type type, object member)
  {
    if (type == (Type) null || member == null)
      return string.Empty;
    if (member is string)
      return member.ToString();
    return !type.GetTypeInfo().IsEnum ? string.Empty : Enum.GetName(type, member);
  }

  public static int GetValue<TEnum>(object member)
  {
    return EnumUtil.GetValue(Common.GetType<TEnum>(), member);
  }

  public static int GetValue(Type type, object member)
  {
    return !string.IsNullOrWhiteSpace(member.SafeString()) ? (int) Enum.Parse(type, member.ToString(), true) : throw new ArgumentNullException(nameof (member));
  }

  public static string GetDescription<TEnum>(object member)
  {
    return Commom.Util.Reflection.GetDescription<TEnum>(EnumUtil.GetName<TEnum>(member));
  }

  public static string GetDescription(Type type, object member)
  {
    return Commom.Util.Reflection.GetDescription(type, EnumUtil.GetName(type, member));
  }

  public static List<Item> GetItems<TEnum>() => EnumUtil.GetItems(typeof (TEnum));

  public static List<Item> GetItems(Type type)
  {
    type = Common.GetType(type);
    if (!type.IsEnum)
      throw new InvalidOperationException($"类型 {type} 不是枚举");
    List<Item> objList = new List<Item>();
    foreach (FieldInfo field in type.GetFields())
      EnumUtil.AddItem(type, (ICollection<Item>) objList, field);
    return objList.OrderBy<Item, int?>((Func<Item, int?>) (t => t.SortId)).ToList<Item>();
  }

  private static void AddItem(Type type, ICollection<Item> result, FieldInfo field)
  {
    if (!field.FieldType.IsEnum)
      return;
    int num = EnumUtil.GetValue(type, (object) field.Name);
    string description = Commom.Util.Reflection.GetDescription((MemberInfo) field);
    result.Add(new Item(description, (object) num, new int?(num)));
  }

  public static List<string> GetNames<TEnum>() => EnumUtil.GetNames(typeof (TEnum));

  public static List<string> GetNames(Type type)
  {
    type = Common.GetType(type);
    if (!type.IsEnum)
      throw new InvalidOperationException($"类型 {type} 不是枚举");
    List<string> names = new List<string>();
    foreach (FieldInfo field in type.GetFields())
    {
      if (field.FieldType.IsEnum)
        names.Add(field.Name);
    }
    return names;
  }
}
